import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:date_format/date_format.dart';

class DatePickerPubDemo extends StatefulWidget {
  DatePickerPubDemo({Key? key}) : super(key: key);

  @override
  State<DatePickerPubDemo> createState() => _DatePickerPubDemoState();
}

class _DatePickerPubDemoState extends State<DatePickerPubDemo> {
  DateTime _nowDate = new DateTime.now();
  DateTime _timePickerData = new DateTime.now();

  void _showDatePicker() {
    DatePicker.showDatePicker(context,
        showTitleActions: true,
        minTime: DateTime(1988, 3, 5),
        maxTime: DateTime(2023, 6, 7),
        theme: DatePickerTheme(
            headerColor: Colors.orange,
            backgroundColor: Colors.blue,
            itemStyle: TextStyle(
                color: Colors.white, fontWeight: FontWeight.bold, fontSize: 18),
            doneStyle: TextStyle(color: Colors.white, fontSize: 16)),
        onChanged: (date) {
      print('change $date in time zone ' +
          date.timeZoneOffset.inHours.toString());
    }, onConfirm: (date) {
      print('confirm $date');
      setState(() {
        this._nowDate = date;
      });
    }, currentTime: DateTime.now(), locale: LocaleType.zh);
  }

  void _showDateTimePicker() {
    DatePicker.showDateTimePicker(context,
        showTitleActions: true,
        minTime: DateTime(2020, 5, 5, 20, 50),
        maxTime: DateTime(2025, 6, 7, 05, 09), onChanged: (date) {
      print('change $date in time zone ' +
          date.timeZoneOffset.inHours.toString());
    }, onConfirm: (date) {
      print('confirm $date');
      setState(() {
        this._timePickerData = date;
      });
    }, locale: LocaleType.zh);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("DatePickerPub"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            InkWell(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text("${formatDate(_nowDate, [yyyy, '-', mm, '-', dd])}"),
                  Icon(Icons.arrow_drop_down)
                ],
              ),
              onTap: _showDatePicker,
            ),
            SizedBox(
              height: 20,
            ),
            InkWell(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text("${formatDate(_timePickerData, [
                        yyyy,
                        '-',
                        mm,
                        '-',
                        dd,
                        " ",
                        HH,
                        ':',
                        nn,
                        ':',
                        ss
                      ])}"),
                  Icon(Icons.arrow_drop_down)
                ],
              ),
              onTap: _showDateTimePicker,
            ),
          ],
        ),
      ),
    );
  }
}
